package com.pipi.biyesheji.service.impl;

import com.google.common.collect.Lists;
import com.pipi.biyesheji.dao.manager.ReviewManager;
import com.pipi.biyesheji.dao.model.ReviewDO;
import com.pipi.biyesheji.dao.query.ReviewQuery;
import com.pipi.biyesheji.dao.support.PageResult;
import com.pipi.biyesheji.enums.AllEnums;
import com.pipi.biyesheji.pojo.Customer;
import com.pipi.biyesheji.pojo.Product;
import com.pipi.biyesheji.pojo.Review;
import com.pipi.biyesheji.pojo.Role;
import com.pipi.biyesheji.service.CustomerService;
import com.pipi.biyesheji.service.ProductService;
import com.pipi.biyesheji.service.ReviewService;
import com.pipi.biyesheji.service.RoleService;
import com.pipi.biyesheji.util.BeanUtils;
import com.pipi.biyesheji.util.Page;
import org.apache.commons.lang.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class ReviewServiceImpl implements ReviewService {

    @Autowired
    private ReviewManager reviewManager;
    @Autowired
    private ProductService productService;
    @Autowired
    private CustomerService customerService;
    @Autowired
    private UserInfo userInfo;
    @Autowired
    private RoleService roleService;

    @Override
    public PageResult<Review> list(Page page) {
        ReviewQuery reviewQuery = new ReviewQuery();
        reviewQuery.setPageNo(page.getStart());
        reviewQuery.setPageSize(page.getCount());
        ReviewQuery.Criteria criteria = reviewQuery.createCriteria();
        // 查询当前登录的信息
        Long userId = userInfo.getCurrentUser().getId();
        // 查询角色
        Role role = roleService.getRoleByUserId(userId);
        // 如果是商家，则只能查询当前商家的评论
        List<Product> productList = Lists.newArrayList();
        if (AllEnums.RoleEnum.BUSINESS.getName().equals(role.getName())) {
            productList = productService.getProductByBid(userId);
            List<Long> productIds = productList.stream().map(Product::getId).collect(Collectors.toList());
            criteria.andPidIn(productIds);
        }
        PageResult<ReviewDO> pageResult = reviewManager.selectByQueryWithPage(reviewQuery);
        Map<Long, Product> productMap = productList.stream().collect(Collectors.toMap(Product::getId, Function.identity()));
        List<Review> reviewList = pageResult.getResult().stream().map(reviewDO -> {
            Review review = buildReview(reviewDO);
            if (AllEnums.RoleEnum.BUSINESS.getName().equals(role.getName())) {
                Product product = productMap.get(review.getPid());
                review.setProduct(product);
            } else {
                Product product = productService.get(review.getPid());
                review.setProduct(product);
            }
            Customer customer = customerService.get(review.getCstid());
            review.setCustomer(customer);
            return review;
        }).collect(Collectors.toList());
        return PageResult.createPageResult(reviewList, pageResult.getPageInfo());
    }

    @Override
    public List<Review> getReviewListByPid(Long id) {
        ReviewQuery reviewQuery = new ReviewQuery();
        reviewQuery.createCriteria().andPidEqualTo(id);
        List<ReviewDO> reviewDOS = reviewManager.selectByQuery(reviewQuery);
        return reviewDOS.stream().map(reviewDO -> {
            Review review = buildReview(reviewDO);
            Customer customer = customerService.get(review.getCstid());
            review.setCustomer(customer);
            return review;
        }).collect(Collectors.toList());
    }

    @Override
    public void del(Long id) {
        reviewManager.deleteByPrimaryKey(id);
    }

    @Override
    public Review get(Long id) {
        ReviewDO reviewDO = reviewManager.selectByPrimaryKey(id);
        return buildReview(reviewDO);
    }

    @Override
    public void save(Review review) {
        ReviewDO reviewDO = BeanUtils.createCloneBean(review, ReviewDO.class);
        reviewManager.insertSelective(reviewDO);
    }

    private List<Review> buildReviews(List<ReviewDO> reviewDOList) {
        return reviewDOList.stream().map(this::buildReview).collect(Collectors.toList());
    }

    private Review buildReview(ReviewDO reviewDO) {
        if (Objects.isNull(reviewDO)) {
            return null;
        }
        Review review = BeanUtils.createCloneBean(reviewDO, Review.class);
        review.setCreatetimeDesc(DateFormatUtils.format(review.getCreatetime(), "yyyy-MM-dd HH:mm:ss"));
        return review;
    }
}
